package com.hhc.controller;

import com.hhc.dto.AverageScoreDTO;
import com.hhc.dto.GradeDTO;
import com.hhc.dto.GradeListQuery;
import com.hhc.entity.GradeRecordDO;
import com.hhc.service.GradeService;
import com.hhc.utils.RestResult;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *   成绩管理控制器，处理 HTTP 请求与响应，定义 API 接口
 * </p>
 * @author pc
 */
@Slf4j
@RestController
@RequestMapping("/grades")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @PostMapping("/record")
    public ResponseEntity<RestResult<GradeRecordDO>> recordStudentGrade(@Validated @RequestBody GradeDTO gradeDTO, @RequestHeader("X-Teacher-Id") Long teacherId) {
        RestResult<GradeRecordDO> result = gradeService.recordStudentGrade(gradeDTO, teacherId);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/list")
    public ResponseEntity<RestResult<List<GradeRecordDO>>> getStudentGradeList(@Validated GradeListQuery gradeListQuery) {
        RestResult<List<GradeRecordDO>> result = gradeService.getStudentGradeList(gradeListQuery);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/average")
    public ResponseEntity<RestResult<AverageScoreDTO>> getCourseAverageScore(@RequestParam("courseId") Long courseId) {
        RestResult<AverageScoreDTO> result = gradeService.getCourseAverageScore(courseId);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/detail")
    public ResponseEntity<RestResult<GradeRecordDO>> getStudentGradeDetail(@RequestParam("gradeRecordId") Long gradeRecordId) {
        RestResult<GradeRecordDO> result = gradeService.getStudentGradeDetail(gradeRecordId);
        return ResponseEntity.ok(result);
    }
}